package net.underrealm.client;

import java.awt.Canvas;
import java.awt.Color;
import java.awt.Graphics;

import net.underrealm.src.*;
import org.lwjgl.LWJGLException;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;
import org.ur_lwg.utils.Interfaces.UnderRealm_Handle;
import org.ur_lwg.utils.Utils;

public class UnderRealm implements Runnable {
	
	public UnderRealm() {
		
	}
	
	@UnderRealm_Handle("main")
	public static void main(String[] args) {
		try {
			Thread mThread = new Thread(new UnderRealm());
			Utils.println("Thread start");
			mThread.start();
			if(mThread.isInterrupted()) {
				HackedThread.handle(ur, "Main");
			}
		} catch (Exception e) {
			e.printStackTrace();
			Utils.error("Main Thread Exception:");
		}
	}

	public void run() {
		try {
			this.startGame();
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}
	
	public void shutdown() {
		Display.destroy();
		System.exit(0);
	}

	@SuppressWarnings("static-access")
	@UnderRealm_Handle("start-game")
	public void startGame() throws LWJGLException {
		if (mainThread) {
			if (uCanvas != null) {
				Graphics g = uCanvas.getGraphics();
				if (g != null) {
					g.setColor(Color.BLUE);
					g.fillRect(0, 0, 765, 500);
					g.dispose();
				}
				Display.setParent(uCanvas);
			}
			if (fullscreen) {

			} else {
				Display.setDisplayMode(new DisplayMode(765, 500));
			}
				Display.setTitle(new GameSettings().clientTitle + " " + new GameSettings().version);
			try {
				Display.create();
				
			} catch (Exception e) {
				e.printStackTrace();
				Display.destroy();
			}
			// Load all other stuff
			this.getOs();
			new Cache(this);
			Utils.println("Client loaded");
			

		}

	}

	public static EnumOS getOs() {
		String s = System.getProperty("os.name").toLowerCase();
		if (s.contains("win")) {
			return EnumOS.windows;
		}
		if (s.contains("mac")) {
			return EnumOS.mac;
		} else {
			return EnumOS.unknown;
		}
	}

	public void runClock() {
		long l1 = System.currentTimeMillis();
		System.out.println(l1);
		
	}

	public boolean mainThread = true;
	public boolean fullscreen = false;
	public static UnderRealm ur;
	public Timer timer;
	public static FileManager fileManager = new FileManager(new UnderRealm());
	public GameSettings gameSettings;
	public int ticksRan;
	public Canvas uCanvas;
}
